
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;


public class Utility {
    
    public static Connection getconnection() 
    
    {
         Connection con = null;
        
        try
        {
         Class.forName("com.mysql.jdbc.Driver");
         con=DriverManager.getConnection("jdbc:mysql:///dbvoting","root","password");
         
        }
        catch(Exception z)
        {          }
        return con;
    }
    public static int getSoleInteger(String query,String columnName)
    {
     String s = "";
        try {
            Statement st = getconnection().createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next())
            {
                s+=rs.getString(columnName)+"/";
            }
        } catch (Exception ex) {
            Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
        }
        return Integer.parseInt(s.split("/")[0]);
    }
    public static String[] getStrings(String query,String columnName)
    {
        
        String s = "";
        try {
            Statement st = getconnection().createStatement();
            ResultSet rs = st.executeQuery(query);
            boolean firstTime = true;
            while (rs.next())
            {
               // if (firstTime){firstTime=false;continue;}
                s+=rs.getString(columnName)+"/";
            }
        } catch (Exception ex) {
            Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally{
            try {
                //rs.close();
                    getconnection().close();
            } catch (SQLException ex) {
                Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
            }
        
        }
        //System.out.println(s);
//        if (s.equals(""))return null;
        return s.split("/");
    }
     public static int[] getIntegers(String query,String columnName)
    {
        String[] str = getStrings(query,columnName);
        int[] ints = new int[str.length];
        for(int x=0;x<str.length;x++)
        {
            ints [x] = Integer.parseInt(str[x]);
        }
        return ints;
    }
    public static void Save(String query)
    {
        int records = 0;
         Statement st = null;
        try {
            st = Utility.getconnection().createStatement();
        } 
        catch (SQLException ex) 
        {          }
        try {
            records = st.executeUpdate(query);
           
            st.close();
        } catch (SQLException ex) 
        { }
       
        if(records!=0)
                {
                    JOptionPane.showMessageDialog(null,"Record has been saved!");
                }
        else
        {
            JOptionPane.showMessageDialog(null,"No Records have been affected");
        }
    }
    public static void Save(String query, boolean maingay)
    {
        int records = 0;
         Statement st = null;
        try {
            st = Utility.getconnection().createStatement();
        } 
        catch (SQLException ex) 
        {          }
        try {
            records = st.executeUpdate(query);
           
            st.close();
        } catch (SQLException ex) 
        { }
       
        if(records!=0 && maingay)
                {
                    JOptionPane.showMessageDialog(null,"Record has been saved!");
                }
        else if (maingay)
        {
            JOptionPane.showMessageDialog(null,"No Records have been affected");
        }
    }
    public static void delete(String query, boolean maingay) 
    {
        int records = 0;
         Statement st = null;
        try {
            st = Utility.getconnection().createStatement();
        } 
        catch (SQLException ex) 
        {          }
        try {
            records = st.executeUpdate(query);
           
            st.close();
        } catch (SQLException ex) 
        { }
       
        if(records!=0 && maingay)
                {
                    JOptionPane.showMessageDialog(null,"Record Deleted!");
                }
        else if (maingay)
        {
            JOptionPane.showMessageDialog(null,"No Records have been affected");
        }
    }
}
